package com.lh.controller;

import com.lh.entity.Result;
import com.lh.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileUploadController {

    @PostMapping("/upload")
    public Result fileUpload(MultipartFile file) throws IOException {
//        获取文件名，并处理文件名
        String name = file.getOriginalFilename();
        String fileName= UUID.randomUUID().toString()+name.substring(name.lastIndexOf("."));
//        获取文件输入流
        InputStream inputStream = file.getInputStream();
        //将文件上传到阿里云服务器，然后获取服务器访问的地址
        String aliUrl = AliOssUtil.uploadFile(fileName, inputStream);
        System.out.println(aliUrl);
        return Result.success(aliUrl);
    }
}
